package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryConfClassItemListReq;
import com.sg.dto.base.component.req.DeleteConfClassItemReq;
import com.sg.dto.base.component.req.QueryConfClassItemDetailReq;
import com.sg.dto.base.component.req.QueryConfClassItemListReq;
import com.sg.entity.OmsConfClassItem;
import com.sg.service.base.component.MOmsConfClassItemService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfClassItemMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfClassItemServiceImpl
        extends ServiceImpl<OmsConfClassItemMapper, OmsConfClassItem>
        implements MOmsConfClassItemService {

    @Autowired
    OmsConfClassItemMapper omsConfClassItemMapper;

    /**
     * 2-3-06查分组下配置项列表[1934]
     * gen by moon at 9/9/2022, 4:23:05 AM
     */
    @Trace(operationName = "2-3-06查分组下配置项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfClassItem> queryConfClassItemList(QueryConfClassItemListReq reqDto) {

        try {
            QueryWrapper<OmsConfClassItem> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfClassItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfClassItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfClassItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfClassItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-06批量新增分组下配置项[2020]
     * gen by moon at 9/9/2022, 4:23:06 AM
     */
    @Trace(operationName = "2-3-06批量新增分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddConfClassItem(List<OmsConfClassItem> omsConfClassItem) {

        if (omsConfClassItem == null || omsConfClassItem.size() == 0) {
            return false;
        }

        return super.saveBatch(omsConfClassItem.stream().map(item -> {
            item.setClassConfItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-3-06批量修改分组下配置项[2046]
     * gen by moon at 9/9/2022, 4:23:07 AM
     */
    @Trace(operationName = "2-3-06批量修改分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateConfClassItem(List<OmsConfClassItem> omsConfClassItem) {

        if (omsConfClassItem == null || omsConfClassItem.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfClassItem);

    }

    /**
     * 2-3-06批量查询分组下配置项列表[2023]
     * gen by moon at 9/9/2022, 4:23:06 AM
     */
    @Trace(operationName = "2-3-06批量查询分组下配置项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfClassItem> batchQueryConfClassItemList(BatchQueryConfClassItemListReq reqDto) {

        try {
            QueryWrapper<OmsConfClassItem> queryWrapper = new QueryWrapper<>();

            if (reqDto.getConfClassItemList() == null || reqDto.getConfClassItemList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_item_class_id", reqDto.getConfClassItemList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfClassItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfClassItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfClassItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfClassItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-06批量删除分组下配置项[1989]
     * gen by moon at 9/9/2022, 4:23:05 AM
     */
    @Trace(operationName = "2-3-06批量删除分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfClassItem(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-06删除分组下配置项[1932]
     * gen by moon at 9/9/2022, 4:23:03 AM
     */
    @Trace(operationName = "2-3-06删除分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfClassItem(DeleteConfClassItemReq reqDto) {
        reqDto.setSubjectLifeCycle("EDITING");
        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-06新增分组下配置项[1930]
     * gen by moon at 9/9/2022, 4:23:02 AM
     */
    @Trace(operationName = "2-3-06新增分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfClassItem(OmsConfClassItem omsConfClassItem) {
        omsConfClassItem.setClassConfItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfClassItem.setSubjectLifeCycle("EDITING");
        omsConfClassItem.setIsArchive("FALSE");
        omsConfClassItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfClassItem.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfClassItem.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfClassItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfClassItem.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfClassItem.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfClassItem.setIsValid("FALSE");
        omsConfClassItem.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfClassItem.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfClassItem)) {
            return omsConfClassItem.getClassConfItemId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-06查询分组下配置项详情[1933]
     * gen by moon at 4/13/2023, 9:12:31 PM
     */
    @Trace(operationName = "2-3-06查询分组下配置项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfClassItem queryConfClassItemDetail(QueryConfClassItemDetailReq reqDto) {

        try {

            QueryWrapper<OmsConfClassItem> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfClassItem> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfClassItem retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-06修改分组下配置项[1931]
     * gen by moon at 9/9/2022, 4:23:03 AM
     */
    @Trace(operationName = "2-3-06修改分组下配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfClassItem(OmsConfClassItem omsConfClassItem) {
        omsConfClassItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfClassItem.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfClassItem.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsConfClassItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfClassItem.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsConfClassItem != null && omsConfClassItem.getSubjectLifeCycle() != null && omsConfClassItem.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfClassItem.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsConfClassItem);
    }
}
